<template>
	<view class="pos-real">
		<u-navbar title="我的钱包" :background="{ backgroundColor: 'transparent' }" :borderBottom="false" titleColor="#fff"
			backIconColor="#fff"></u-navbar>

		<image src="https://7463-tcb-zoumbaexjdidjmj2ff212-5d3479-1303238310.tcb.qcloud.la/static/img/nav_img_bg@2x.png"
			mode="scaleToFill" style="width: 100%; height: 750rpx;" class="pos-abs-lt"></image>
		<view class="content">
			<view class="content-item-1">
				<image
					src="https://7463-tcb-rxeszamj94676b-6d6j813d8fcf0-1303238310.tcb.qcloud.la/static/img/fenxiaoimg.png"
					mode="scaleToFill" style="width: 690rpx; height: 508rpx;" class="pos-abs"></image>
				<view class="content-money">
					<view style="margin-left: 25rpx;margin-right: 25rpx;margin-bottom: 15rpx;padding-top:15rpx">
						<view class="uni-flex justify-between">
							<view style="color: white;font-size: 36rpx;font-weight: 600;">分销总资产</view>
							<view class="uni-flex align-center">
								<view style="color: white;font-size: 36rpx;font-weight: 600;">
									{{ ((userInfo.account.total || 0) / 100).toFixed(2) }}
								</view>
								<view style="color: white;"> (元)</view>
							</view>
						</view>
					</view>
					<view class="uni-flex justify-center">
						<view
							style="width:306rpx;height: 114rpx;background-color: #FFFFFF;opacity: 0.4;border: 2rpx solid #FFFFFF;border-radius: 20rpx;margin-right: 20rpx;">
							<view class="uni-flex uni-column justify-center align-center">
								<view style="color: #BE311C;font-weight: 600;">
									￥{{ ((userInfo.personalTotalIncome || 0) / 100).toFixed(2) }}</view>
								<view style="color: #BE311C;font-weight: 600;">个人总收益</view>
							</view>
						</view>
						<view
							style="width:306rpx;height: 114rpx;background-color: #FFFFFF;opacity: 0.4;border: 2rpx solid #FFFFFF;border-radius: 20rpx;">
							<view class="uni-flex uni-column justify-center align-center">
								<view style="color: #BE311C;font-weight: 600;">
									￥{{ ((userInfo.teamTotalIncome || 0) / 100).toFixed(2) }}</view>
								<view style="color: #BE311C;font-weight: 600;">团队总收益</view>
							</view>
						</view>
					</view>
					<view style=" margin: 40rpx 40rpx 10rpx 40rpx;">
						<view class="uni-flex justify-between align-center">
							<view class="uni-flex uni-column align-center">
								<view style="font-size: 30rpx;font-weight: 600;">
									￥{{ ((userInfo.account.balance || 0) / 100).toFixed(2) }}</view>
								<view style="color: rgba(153, 153, 153, 1);">可提现余额</view>
							</view>
							<view style="color: #CCCCCC;">|</view>
							<view class="uni-flex uni-column align-center">
								<view style=" font-size: 30rpx;font-weight: 600;">
									￥{{ ((userInfo.account.settle || 0) / 100).toFixed(2) }}</view>
								<view style="color: rgba(153, 153, 153, 1);">待结算</view>
							</view>
							<view style="color: #CCCCCC;">|</view>
							<view class="uni-flex uni-column align-center">
								<view style="font-size: 30rpx;font-weight: 600;">
									￥{{ ((userInfo.account.frozen || 0) / 100).toFixed(2) }}</view>
								<view style="color: rgba(153, 153, 153, 1);">冻结余额</view>
							</view>
						</view>
					</view>
					<view
						style="height: 70rpx;background-color: rgba(255, 125, 102, 1);border-radius: 40rpx;margin-left: 25rpx;margin-right: 25rpx;">
						<view @click="clickWithdraw" class="uni-flex"
							style="width:100%;height: 100%;;justify-content: center;align-items: center">
							<view style="color: rgba(255, 255, 255, 1);font-weight: 600;">
								去提现
							</view>
						</view>
					</view>
					<view @click="clickRecord" class="uni-flex justify-center" style="margin-top: 7rpx;">
						<view style="color:rgba(153, 153, 153, 1)">查看提现明细></view>
					</view>
					<view style="margin-top: 50rpx;">
						<image
							src="https://7463-tcb-rxeszamj94676b-6d6j813d8fcf0-1303238310.tcb.qcloud.la/static/img/chongzhiimg.png"
							mode="scaleToFill" style="width: 690rpx; height: 372rpx;" class="pos-abs"></image>
						<view class="content-money">
							<view style="margin:15rpx 25rpx 15rpx 25rpx;">
								<!-- <view style="width: 8rpx;height: 32rpx;background-color: white;"></view> -->
								<view class="uni-flex justify-between">
									<view style="color: white;font-size: 36rpx;font-weight: 600;">充值余额</view>
									<view class="uni-flex align-center">
										<view style="color: white;font-size: 36rpx;font-weight: 600;">
											{{(disCountInfo.amountSum/100).toFixed(2)}}
										</view>
										<view style="color: white;"> (元)</view>
									</view>
								</view>
							</view>
							<view class="uni-flex justify-center">
								<view
									style="width:306rpx;height: 114rpx;background-color: #FFFFFF;opacity: 0.4;border: 2rpx solid #FFFFFF;border-radius: 20rpx;margin-right: 20rpx;">
									<view class="uni-flex uni-column justify-center align-center">
										<view style="color: #B45800;font-weight: 600;">
											￥{{((disCountInfo.topupSum)/100).toFixed(2)}}</view>
										<view style="color: #B45800;font-weight: 600;">累计充值</view>
									</view>
								</view>
								<view
									style="width:306rpx;height: 114rpx;background-color: #FFFFFF;opacity: 0.4;border: 2rpx solid #FFFFFF;border-radius: 20rpx;">
									<view class="uni-flex uni-column justify-center align-center">
										<view style="color: #B45800;font-weight: 600;">
											￥{{(disCountInfo.consumeSum/100).toFixed(2)}}</view>
										<view style="color: #B45800;font-weight: 600;">累计消费</view>
									</view>
								</view>
							</view>
							<view @click="clickDetail"
								style="margin-top: 70rpx;height: 70rpx;border-radius: 40rpx;margin-left: 25rpx;margin-right: 25rpx;border: 1rpx solid #FF9F43;">
								<view class="uni-flex"
									style="width:100%;height: 100%;justify-content: center;align-items: center">
									<view style="color: #FF9F43;font-weight: 600;">
										查看明细
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
	</view>

</template>

<script>
	import {
		mapState
	} from 'vuex';
	import {
		getUserInfo,
		getUserPackageSearch
	} from "@/api/platform/user.js";
	import {
		genPackageOrder,
		genPayInfo,
		updatePakcageState,
		getQrcode
	} from "@/api/platform/order.js";
	const db = uniCloud.database()
	export default {
		data() {
			return {
				userInfo: {
					account: {
						total: 0,
						balance: 0,
						settle: 0,
						frozen: 0
					}
				},
				disCountInfo: {
					consumeSum: 0,
					giveSum: 0,
					topupSum: 0,
					amountSum: 0
				}
			}
		},
		computed: {
			...mapState(["updateUserInfo"])
		},
		async onLoad(e) {

			console.log("q", e)

			// this.packagePay('80e3bed065a64123076b6abe41db3150')
			if (e.q) {
				let packageId = decodeURIComponent(e.q).split('?qrcodeId=')[1]
				await this.getQrcode(packageId)
			}
			//await this.getQrcode('34e8707565bf326f002a22d855436f59')
		},
		async onReady() {
			uni.showLoading({
				mask: true
			})

			await this.getDisCountInfo()
			await this.getData()
			uni.hideLoading()
		},
		async onPullDownRefresh() {

			await this.getDisCountInfo()
			await this.getData();
			uni.stopPullDownRefresh();
		},
		methods: {
			clickRecharge() {

			},
			clickWithdraw() {
				uni.navigateTo({
					url: "./withdraw"
				})
			},
			clickDetail() {
				uni.navigateTo({
					url: "./package/topup"
				})
			},
			clickRecord() {
				uni.navigateTo({
					url: './withdraw-list'
				})
			},
			clickInvoice() {
				uni.navigateTo({
					url: './invoice-apply-list'
				})
			},

			async getData() {
				try {
					let currentRegion = uni.getStorageSync('currentRegion')
					let currentCityCode = currentRegion.city.value
					this.userInfo = await getUserInfo({
						showLoading: false
					}, currentCityCode);
					this.updateUserInfo(this.userInfo)
				} catch (e) {
					uni.hideLoading()
				}
			},
			async getDisCountInfo() {
				await getUserPackageSearch({
					showLoading: false
				}).then(res => {
					console.log("res", res)
					if (res.consumeSum.data[0]) {
						this.disCountInfo.consumeSum = res.consumeSum.data[0].totalCount
					}
					if (res.giveSum.data[0]) {
						this.disCountInfo.giveSum = res.giveSum.data[0].totalCount
					}
					if (res.topupSum.data[0]) {
						this.disCountInfo.topupSum = res.topupSum.data[0].totalCount
					}
					if (res.amountSum.data[0]) {
						this.disCountInfo.amountSum = res.amountSum.data[0].totalCount
					}
					console.log("11", this.disCountInfo)
				})
			},

			async getQrcode(id) {
				console.log("qrcode",id)
				uni.showLoading({
					mask: true,
					title: '获取套餐中'
				})
				const qr = await getQrcode({
					packageId: id,
					showLoading: false
				})
				await this.packagePay(qr)
			},

			async packagePay(packageId) {
				console.log("packagePay", packageId)
				try {
					var orderPayInfo;
					const order = await genPackageOrder({
						packageId: packageId,
						showLoading: false
					});
					orderPayInfo = await genPayInfo({
						orderId: order._id,
						showLoading: false
					});
					console.log("234", orderPayInfo)
					uni.showLoading({
						mask: true,
						title: '检测支付环境'
					})
					// await updatePakcageState({
					// 	orderId: packageId,
					// 	user: this.userInfo,
					// 	showLoading: false
					// });


					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: orderPayInfo.timeStamp,
						nonceStr: orderPayInfo.nonceStr,
						package: orderPayInfo.package,
						signType: orderPayInfo.signType,
						paySign: orderPayInfo.paySign,
						success: async (res) => {
							console.log('支付成功', res)
							//let success = await this.finishPay(order)
							await this.getDisCountInfo()
							uni.hideLoading()
							this.getData()
						},
						fail: (err) => {
							console.log('用户取消支付', err)
							uni.hideLoading()
						}
					})
				} catch (e) {
					console.log(e)
					uni.hideLoading()
					//TODO handle the exception
				}

			},
			async finishPay(order) {
				try {
					order = await getOrderDetail({
						id: order._id
					})
					// if (order.status === 5) {
					// 	let currentRegion = uni.getStorageSync('currentRegion')
					// 	let currentCityCode = currentRegion.city.value
					// 	let userInfo = await getUserInfo({}, currentCityCode)
					// 	this.updateUserInfo(userInfo)
					// 	return true
					// }
				} catch (e) {
					console.error(e)
					uni.showModal({
						content: '查询支付状态遇到错误！',
						cancelText: '联系客服',
						confirmText: '重试',
						success: (res) => {
							if (res.confirm) {
								this.finishPay(order)
							} else {
								this.$copy("i3d008", () => {
									uni.showModal({
										content: '客服微信已复制，请添加客服好友',
										showCancel: false,
										confirmText: '朕知道了'
									})
								})
							}
						}
					})
				}
			},
		}
	}
</script>

<style>
	page {
		background-color: #F5F5F5;
	}
</style>

<style lang="scss" scoped>
	.content {
		margin-top: 560rpx;
		padding-bottom: 60rpx;

		.content-money {
			height: 100%;
			position: relative;
			z-index: 9;
		}

		.content-item-1 {
			width: 690rpx;
			height: 170rpx;
			margin: 0 30rpx;
		}

		.content-item-2 {
			width: 330rpx;
			height: 130rpx;
		}

		.content-operate {
			background: #FFFFFF;
			border-radius: 24rpx;
			margin: 60rpx 30rpx 0 30rpx;
			padding: 0 30rpx;

			.content-operate-item:not(:last-child) {
				border-bottom: 1px solid #EAEAEA;
				;
			}
		}

		.content-btn {
			margin: 88rpx 30rpx 0 30rpx;

			.content-btn-0 {
				width: 330rpx;
				height: 88rpx;
				border-radius: 50rpx;
				font-size: 30rpx;
				color: white;
			}

			.content-btn-1 {
				background-color: #CFCFCF;
			}

			.content-btn-2 {
				background: #FF5673;
			}
		}

		.content-explain {
			margin: 60rpx 30rpx 0 30rpx;
			background: #E9E9E9;
			border-radius: 24rpx;
			padding: 30rpx;

			.content-explain-icon {
				width: 32rpx;
				height: 32rpx;
				filter: grayscale(1);
			}

			.content-explain-title {
				font-size: 30rpx;
				color: #5A5A5C;
			}
		}
	}
</style>